// pages/laigao/index.js
import ImgListCell from '../../template/ImgListCell';
const util = require('../../utils/util.js');
const apiUrl = require('../../utils/apiUrl.js');
var http = require('../../utils/httpHelper.js');

Page({
  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    navSelectIndex: 0,
    page: 1,
    listData: [],
    // 显示加载更多 loading
    hothidden: true,
    keyword: "",
    isTouchstart: false,
    isLongtap: false,
    isTouchmove: false,
    isLiveShow: false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    Object.assign(this, ImgListCell);

    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });

    this.loadListData();
  },
  onShow:function(){
    var isCheck = wx.getStorageSync("isCheck");
    this.setData({
      isLiveShow: !isCheck,
    });
  },
  searchContentInput: function (e) {
    this.setData({
      keyword: e.detail.value
    })
  },
  searchSubmit: function () {
    var that = this;
    that.loadListData();
  },
  onPullDownRefresh: function () {
    // 页面相关事件处理函数--监听用户下拉动作
    this.loadListData();
  },
  onReachBottom: function () {
    // 页面上拉触底事件的处理函数
    var that = this;

    // 加载更多 loading
    that.setData({
      hothidden: true
    })

    var page = that.data.page + 1;

    var formData = {};

    //来稿小伙伴的文章/直播列表
    var url = apiUrl.UrlLaigaoNewsList;

    if (that.data.navSelectIndex > 1) {
      //来稿用户小伙伴列表
      url = apiUrl.UrlLaigaoUserList;
      if (that.data.navSelectIndex == 3) {
        //所有的
        formData['type'] = 1;
      } else {
        //已绑定的
        formData['type'] = 2;
      }

      // formData['pagesize'] = 10;

      formData['keyword'] = that.data.keyword;

    } else {
      if (that.data.navSelectIndex == 0) {
        //文章
        formData['type'] = 1;
      } else {
        //直播
        formData['type'] = 2;
      }
    }

    formData['page'] = page;

    http.ApiGet(url, formData, function (res) {
      var listData = res.list;
      // 获取当前现有数据进行保存
      var list = that.data.listData;
      if (listData && listData.length > 0) {

        listData = listData.map(function (item) {

          if (item.title) {
            if (item.thumb.length > 0) {
              item.thumb = [item.thumb];
            } else {
              item.thumb = [];
            }
            item.autor = item.nickname ? item.nickname : "";
            item.ptime = util.getDateDiff(item.create_time + "000");
          }

          return item;
        })

        that.setData({
          listData: list.concat(listData),
          page: page,
          hothidden: false
        });
      } else {
        that.setData({
          hothidden: false
        });
      }
      wx.hideNavigationBarLoading() //完成停止加载
    });

  },
  loadListData: function () {
    var that = this;
    var formData = {};

    //来稿小伙伴的文章/直播列表
    var url = apiUrl.UrlLaigaoNewsList;

    if (that.data.navSelectIndex > 1) {
      //来稿用户小伙伴列表
      url = apiUrl.UrlLaigaoUserList;
      if (that.data.navSelectIndex == 3) {
        //所有的
        formData['type'] = 1;
      } else {
        //已绑定的
        formData['type'] = 2;
      }
      
      // formData['pagesize'] = 10;

      formData['keyword'] = that.data.keyword;

    } else {
      if (that.data.navSelectIndex == 0) {
        //文章
        formData['type'] = 1;
      } else {
        //直播
        formData['type'] = 2;
      }
    }

    formData['page'] = 1;

    http.ApiGet(url, formData, function (res) {
      var listData = res.list;
      // console.log(res.data);
      if (listData && listData.length > 0) {

        listData = listData.map(function (item) {

          if (item.title) {
            if (item.thumb.length > 0) {
              item.thumb = [item.thumb];
            } else {
              item.thumb = [];
            }
            item.autor = item.nickname ? item.nickname : "";
            item.ptime = util.getDateDiff(item.create_time + "000");
          }

          return item;
        })

        that.setData({
          listData: listData,
          page: 1,
          hothidden: false
        });
      } else {
        that.setData({
          listData: [],
          hothidden: false
        });
      }
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    });
  },
  //按下事件开始  
  doTouchstart: function (e) {
    let that = this;

    var index = e.currentTarget.dataset.index;

    var theItem = that.data.listData[index];

    that.setData({
      isTouchstart: true,
      isLongtap: false
    })

    setTimeout(function () {
      if (that.data.isTouchstart && !that.data.isTouchmove) {
        that.setData({
          isLongtap: true
        })

        // console.log("到时间了,是长按");

        var title = theItem.title;//"文章：" + 

        var itemList = [title, '直接采用', '编辑采用'];

        var postData = {};

        postData["type"] = 1;
        postData["id"] = theItem.id;

        if (that.data.navSelectIndex == 1) {
          // title = "直播：" + theItem.title;
          itemList = [title, '直接采用'];
          postData["type"] = 2;
        }

        wx.showActionSheet({
          itemList: itemList,
          success: function (res) {
            if (!res.cancel) {
              var text = itemList[res.tapIndex];

              if (text == "直接采用") {
                console.log(text);

                that.doUse(postData);

              } else if (text == "编辑采用") {
                console.log(text);

                wx.navigateTo({
                  url: '../news/submit?type=1&title=提交文章&lgid='+theItem.id
                })
              }
            }
          }
        })

      }
    }, 500);
  },
  //按下事件结束  
  doTouchend: function (e) {
    let that = this;
    var index = e.currentTarget.dataset.index;
    var theItem = that.data.listData[index];

    that.setData({
      isTouchstart: false
    })

    if (!that.data.isLongtap && !that.data.isTouchmove) {
      console.log("点击");

      var link = "https://laigao.longsunhd.com/api/html/article/article_id/" + theItem.id + "/client/1";


      if (that.data.navSelectIndex == 1) {
        link = "https://laigao.longsunhd.com/api/html/live/live_id/" + theItem.id + "/client/1";
      }

      wx.navigateTo({
        url: '../webview/index?url=' + link
      })
      //跳转打开文章、直播
    }
    that.setData({
      isTouchmove: false
    });

  },
  doTouchmove: function (e) {
    let that = this;
    that.setData({
      isTouchmove: true
    })
    console.log("滑动");
  },
  tapAtIndex: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var theItem = that.data.listData[index];

    // console.log(theItem.uid);
    if (that.data.navSelectIndex == 3) {
      //所有的可绑定
      wx.showModal({
        title: theItem.nickname,
        cancelText: '查看主页',
        confirmText: '绑定',
        success: function (res) {
          if (res.cancel) {
            // console.log('用户点击查看主页');
            var link = "https://laigao.longsunhd.com/api/html/index/uid/" + theItem.uid + "/min_app/1";
            wx.navigateTo({
              url: '../webview/index?url=' + link
            })
          }
          if (res.confirm) {
            // console.log('用户点击绑定');

            var postData = {};

            postData["uid"] = theItem.uid;
            postData["nickname"] = theItem.nickname;

            that.doBind(postData);
          }
        }
      });
    } else {
      var link = "https://laigao.longsunhd.com/api/html/index/uid/" + theItem.uid + "/min_app/1";
      wx.navigateTo({
        url: '../webview/index?url=' + link
      })
      // console.log('查看主页');
    }
  },
  doUse: function (postData) {
    http.ApiPost(apiUrl.UrlLaigaoNewsUse, postData, function (res) {
      if (res.result.errorCode == 1) {
        wx.showToast({
          title: '采用成功！',
          icon: 'success',
          duration: 1000
        });
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  doBind: function (postData) {
    http.ApiPost(apiUrl.UrlLaigaoUserBind, postData, function (res) {
      if (res.result.errorCode == 1) {
        wx.showToast({
          title: '绑定成功！',
          icon: 'success',
          duration: 1000
        });
      } else {
        wx.showModal({
          title: res.result.errorMessage
        });
      }
    });
  },
  navItemTap: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.id;
    that.setData({
      navSelectIndex: index,
      page: 1
    });

    that.loadListData();
  }
})